/*
 * @(#) AwardSheetController.java
 * Copyright (c) 2011 EdRP, Dayalbagh Educational Institute.
 * All Rights Reserved.
 *
 * Redistribution and use in source and binary forms, with or
 * without modification, are permitted provided that the following
 * conditions are met:
 *
 * Redistributions of source code must retain the above copyright
 * notice, this  list of conditions and the following disclaimer.
 *
 * Redistribution in binary form must reproducuce the above copyright
 * notice, this list of conditions and the following disclaimer in
 * the documentation and/or other materials provided with the
 * distribution.
 *
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL ETRG OR ITS CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL,SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * Contributors: Members of EdRP, Dayalbagh Educational Institute
 */
package in.ac.dei.edrp.cms.controller.awardsheet;

import in.ac.dei.edrp.cms.common.utility.MyException;
import in.ac.dei.edrp.cms.dao.awardsheet.AwardSheetDao;
import in.ac.dei.edrp.cms.domain.awardsheet.AwardSheetInfoGetter;
import in.ac.dei.edrp.cms.domain.entity.EntityInfoGetter;
import in.ac.dei.edrp.cms.domain.programmaster.ProgramMasterInfoGetter;
import in.ac.dei.edrp.cms.domain.programmaster.ProgramTermDetailsInfoGetter;

import java.util.List;
import java.util.StringTokenizer;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;


/**
 * Controller for Award Blank Sheet
 * @author Manpreet Kaur
 * @date 20-03-2011
 * @version 1.0
 */
public class AwardSheetController extends MultiActionController {
    AwardSheetDao awardSheetDao;

    /**
     * Method to initialize controller
     * @param awardSheetDao object of AwardSheetDao interface
     */
    public void setAwardSheetDao(AwardSheetDao awardSheetDao) {
        this.awardSheetDao = awardSheetDao;
        
        
    }

    /**
     *  Method to get list of semesters available in university
     * @param request
     * @param response
     * @return object of ModelAndView
     * @throws Exception
     */
    public ModelAndView getEntityList(HttpServletRequest request,
        HttpServletResponse response) throws Exception {
        EntityInfoGetter inputObj = new EntityInfoGetter();
        HttpSession session=request.getSession(true);
        inputObj.setUniversityId(session.getAttribute("universityId").toString());
        inputObj.setEntityType(request.getParameter("entityType"));

        List<ProgramTermDetailsInfoGetter> entityList = awardSheetDao.getEntityList(inputObj);

        return new ModelAndView("termdetails/SysTwoList", "result", entityList);
    }

    /**
     *  Method to get list of semesters available in university
     * @param request
     * @param response
     * @return object of ModelAndView
     * @throws Exception
     */
    public ModelAndView getProgramList(HttpServletRequest request,
        HttpServletResponse response) throws Exception {
        EntityInfoGetter inputObj = new EntityInfoGetter();
        inputObj.setEntityId(request.getParameter("entityId"));

        List<ProgramTermDetailsInfoGetter> progList = awardSheetDao.getProgramList(inputObj);

        return new ModelAndView("termdetails/SysTwoList", "result", progList);
    }

    /**
     *  Method to get list of semesters available in university
     * @param request
     * @param response
     * @return object of ModelAndView
     * @throws Exception
     */
    public ModelAndView getBranchList(HttpServletRequest request,
        HttpServletResponse response) throws Exception {
        EntityInfoGetter inputObj = new EntityInfoGetter();
        HttpSession session=request.getSession(true);
        inputObj.setEntityId(request.getParameter("entityId"));
        inputObj.setUniversityId(session.getAttribute("universityId").toString());
        inputObj.setProgramId(request.getParameter("programId"));

        List<ProgramTermDetailsInfoGetter> branchList = awardSheetDao.getBranchList(inputObj);

        return new ModelAndView("termdetails/SysTwoList", "result", branchList);
    }

    /**
     *  Method to get list of semesters available in university
     * @param request
     * @param response
     * @return object of ModelAndView
     * @throws Exception
     */
    public ModelAndView getSpecializationList(HttpServletRequest request,
        HttpServletResponse response) throws Exception {
        ProgramMasterInfoGetter inputObj = new ProgramMasterInfoGetter();
        HttpSession session=request.getSession(true);
        inputObj.setEntityId(request.getParameter("entityId"));
        inputObj.setUniversityId(session.getAttribute("universityId").toString());
        inputObj.setProgramId(request.getParameter("programId"));
        inputObj.setBranchcode(request.getParameter("branchCode"));

        List<ProgramTermDetailsInfoGetter> specList = awardSheetDao.getSpecializationList(inputObj);

        return new ModelAndView("termdetails/SysTwoList", "result", specList);
    }

    /**
     *  Method to get list of semesters available in university
     * @param request
     * @param response
     * @return object of ModelAndView
     * @throws Exception
     */
    public ModelAndView getSemesterList(HttpServletRequest request,
        HttpServletResponse response) throws Exception {
        ProgramMasterInfoGetter inputObj = new ProgramMasterInfoGetter();
        HttpSession session=request.getSession(true);
        inputObj.setUniversityId(session.getAttribute("universityId").toString());
        inputObj.setProgramId(request.getParameter("programId"));

        List<ProgramTermDetailsInfoGetter> semList = awardSheetDao.getSemesterList(inputObj);

        return new ModelAndView("awardsheet/SemesterList", "result", semList);
    }

    /**
     *  Method to get list of semesters available in university
     * @param request
     * @param response
     * @return object of ModelAndView
     * @throws Exception
     */
    public ModelAndView getCourseList(HttpServletRequest request,
        HttpServletResponse response) throws Exception {
        ProgramMasterInfoGetter inputObj = new ProgramMasterInfoGetter();
        HttpSession session=request.getSession(true);
        inputObj.setProgramId(request.getParameter("programId"));
        inputObj.setBranchcode(request.getParameter("branchCode"));
        inputObj.setSpecializationCode(request.getParameter("specCode"));
        inputObj.setSystemCode(request.getParameter("semesterCode"));
        inputObj.setStartdate(request.getParameter("startDate"));
        inputObj.setEndDate(request.getParameter("endDate"));
        inputObj.setCreatorId(session.getAttribute("userId").toString());
        inputObj.setUniversityId(request.getParameter("programId").substring(0, 4));
    

        List<ProgramTermDetailsInfoGetter> courseList = awardSheetDao.getCourseList(inputObj);

        return new ModelAndView("termdetails/SysTwoList", "result", courseList);
    }

    /**
     *  Method to get list of evaluation components for given course
     * @param request
     * @param response
     * @return object of ModelAndView
     * @throws Exception
     */
    public ModelAndView getEvaluationComponents(HttpServletRequest request,
        HttpServletResponse response) throws Exception {
        AwardSheetInfoGetter inputObj = new AwardSheetInfoGetter();
        HttpSession session=request.getSession(true);
        inputObj.setProgramId(request.getParameter("programId"));
        inputObj.setUniversityId(session.getAttribute("universityId").toString());
        inputObj.setCourseCode(request.getParameter("courseCode"));
        inputObj.setDisplayType(request.getParameter("displayType"));

        List<AwardSheetInfoGetter> componentList = awardSheetDao.getEvaluationComponents(inputObj);

        return new ModelAndView("awardsheet/EvaluationComponentList", "result",
            componentList);
    }

    /**
     *  Method to get list of students for given course
     * @param request
     * @param response
     * @return object of ModelAndView
     * @throws Exception
     */
    public ModelAndView getStudentList(HttpServletRequest request,
        HttpServletResponse response) throws Exception {
        ProgramMasterInfoGetter inputObj = new ProgramMasterInfoGetter();
        inputObj.setProgramId(request.getParameter("programId"));
        inputObj.setBranchcode(request.getParameter("branchCode"));
        inputObj.setSpecializationCode(request.getParameter("specCode"));
        inputObj.setSystemCode(request.getParameter("semesterCode"));
        inputObj.setCourseCode(request.getParameter("courseCode"));
        inputObj.setDisplayType(request.getParameter("displayType"));

        List<AwardSheetInfoGetter> studentList = awardSheetDao.getStudentList(inputObj);

        return new ModelAndView("awardsheet/StudentList", "result", studentList);
    }

    /**
     *  Method to get list of marks of students for given course
     * @param request
     * @param response
     * @return object of ModelAndView
     * @throws Exception
     */
    public ModelAndView getStudentMarks(HttpServletRequest request,
        HttpServletResponse response) throws Exception {
        ProgramMasterInfoGetter inputObj = new ProgramMasterInfoGetter();
        HttpSession session=request.getSession(true);
        inputObj.setUniversityId(session.getAttribute("universityId").toString());
        inputObj.setProgramId(request.getParameter("programId"));
        inputObj.setBranchcode(request.getParameter("branchCode"));
        inputObj.setSpecializationCode(request.getParameter("specCode"));
        inputObj.setSystemCode(request.getParameter("semesterCode"));
        inputObj.setCourseCode(request.getParameter("courseCode"));
        inputObj.setStartdate(request.getParameter("startDate"));
        inputObj.setEndDate(request.getParameter("endDate"));
        inputObj.setSystemValue(request.getParameter("buttonPressed"));
        System.out.println("button pressed is "+request.getParameter("buttonPressed"));
             
        List<AwardSheetInfoGetter> marksList = awardSheetDao.getStudentMarks(inputObj);

        return new ModelAndView("awardsheet/MarksList", "result", marksList);
    }

    /**
     *  Method to get list of marks of students for given course
     * @param request
     * @param response
     * @return object of ModelAndView
     * @throws Exception
     */
    public ModelAndView getRule(HttpServletRequest request,
        HttpServletResponse response) throws Exception {
        ProgramMasterInfoGetter inputObj = new ProgramMasterInfoGetter();
        HttpSession session=request.getSession(true);
        inputObj.setUniversityId(session.getAttribute("universityId").toString());

        List<AwardSheetInfoGetter> rule = awardSheetDao.getStudentMarks(inputObj);

        return new ModelAndView("termdetails/SysTwoList", "result", rule);
    }

    /**
    *   Method for checking status of existing entry regarded given course
    * @param request
    * @param response
    * @return object of ModelAndView
    * @throws Exception
    */
    public ModelAndView getStatus(HttpServletRequest request,
        HttpServletResponse response) throws Exception {
        ProgramMasterInfoGetter inputObj = new ProgramMasterInfoGetter();
        inputObj.setProgramCourseKey(request.getParameter("programCourseKey"));
        inputObj.setCourseCode(request.getParameter("courseCode"));
        inputObj.setStartdate(request.getParameter("startDate"));
        inputObj.setEndDate(request.getParameter("endDate"));
        inputObj.setEntityId(request.getParameter("entityId"));
        inputObj.setCreatorId(request.getParameter("employeeCode"));
        inputObj.setUniversityId(request.getParameter("entityId").substring(0, 4));
        inputObj.setDisplayType(request.getParameter("displayType"));
        
      

        String statusValue = awardSheetDao.checkStatus(inputObj);

        return new ModelAndView("RegistrationForm/RegisterStudent", "result", statusValue);
    }

    /**
     *   Method for getting employee code
     * @param request
     * @param response
     * @return object of ModelAndView
     * @throws Exception
     */
    public ModelAndView getEmployeeCode(HttpServletRequest request,
        HttpServletResponse response) throws Exception {
//        String userId = "E00010001";
        HttpSession session = request.getSession(true);
        
//        
//        System.out.println("from session "+session.getAttribute("userId"));

        List<ProgramTermDetailsInfoGetter> employeeCode = awardSheetDao.getEmployeeCode(session.getAttribute("userId").toString());

        return new ModelAndView("termdetails/SysTwoList", "result", employeeCode);
    }

    /**
     *   Method for getting program course key
     * @param request
     * @param response
     * @return object of ModelAndView
     * @throws Exception
     */
    public ModelAndView getProgramCourseKey(HttpServletRequest request,
        HttpServletResponse response) throws Exception {
        ProgramMasterInfoGetter inputObj = new ProgramMasterInfoGetter();
        HttpSession session=request.getSession(true);
        inputObj.setUniversityId(session.getAttribute("universityId").toString());
        inputObj.setProgramId(request.getParameter("programId"));
        inputObj.setBranchcode(request.getParameter("branchCode"));
        inputObj.setSpecializationCode(request.getParameter("specCode"));
        inputObj.setSystemCode(request.getParameter("semesterCode"));

        List<ProgramTermDetailsInfoGetter> pck = awardSheetDao.getProgramCourseKey(inputObj);

        return new ModelAndView("termdetails/SysTwoList", "result", pck);
    }

    /**
     *   Method for getting list of pending requests
     * @param request
     * @param response
     * @return object of ModelAndView
     * @throws Exception
     */
    public ModelAndView getPendingRequestList(HttpServletRequest request,
        HttpServletResponse response) throws Exception {
        ProgramMasterInfoGetter inputObj = new ProgramMasterInfoGetter();
        HttpSession session=request.getSession(true);
        inputObj.setUniversityId(session.getAttribute("universityId").toString());
        inputObj.setCreatorId(request.getParameter("employeeCode"));
        inputObj.setDisplayType(request.getParameter("displayType"));
       

        List<ProgramTermDetailsInfoGetter> pendingList = awardSheetDao.getPendingList(inputObj);
        return new ModelAndView("awardsheet/PendingList", "result", pendingList);
    }

    /**
     * Method for insert/update student marks
     * @param request
     * @param response
     * @return object of ModelAndView
     * @throws Exception
     */
    public ModelAndView saveStudentMarks(HttpServletRequest request,
        HttpServletResponse response) throws Exception {
        AwardSheetInfoGetter inputObj = new AwardSheetInfoGetter();
        HttpSession session=request.getSession(true);
        inputObj.setUniversityId(session.getAttribute("universityId").toString());
        inputObj.setProgramCourseKey(request.getParameter("programCourseKey"));
        inputObj.setCourseCode(request.getParameter("courseCode"));
        inputObj.setStartDate(request.getParameter("startDate"));
        inputObj.setEndDate(request.getParameter("endDate"));
        inputObj.setDisplayType(request.getParameter("displayType"));
        inputObj.setCreatorId(request.getParameter("employeeCode"));
        
      

        StringTokenizer data = new StringTokenizer(request.getParameter("data"), ";");

        String statusValue = null;

        try {
            statusValue = awardSheetDao.saveStudentMarks(inputObj, data);
        } catch (MyException e) {

            return new ModelAndView("RegistrationForm/RegisterStudent","result", e.getMessage());
        }

        return new ModelAndView("RegistrationForm/RegisterStudent", "result", statusValue);
    }

    /**
     * Method for submitting award sheet for approval
     * @param request
     * @param response
     * @return object of ModelAndView
     * @throws Exception
     */
    public ModelAndView submitForApproval(HttpServletRequest request,
        HttpServletResponse response) throws Exception {
        AwardSheetInfoGetter inputObj = new AwardSheetInfoGetter();

        inputObj.setProgramCourseKey(request.getParameter("programCourseKey"));
        inputObj.setEntityId(request.getParameter("entityId"));
        inputObj.setCourseCode(request.getParameter("courseCode"));
        inputObj.setStartDate(request.getParameter("startDate"));
        inputObj.setEndDate(request.getParameter("endDate"));
        inputObj.setEmployeeCode(request.getParameter("employeeCode"));
        inputObj.setStatus(request.getParameter("status"));
        inputObj.setDisplayType(request.getParameter("displayType"));
        
        
        String result = null;

        try {
            result = awardSheetDao.submitForApproval(inputObj, "S");
        } catch (MyException e) {

            return new ModelAndView("RegistrationForm/RegisterStudent",
                "result", e.getMessage());
        }

        return new ModelAndView("RegistrationForm/RegisterStudent", "result",
            result);
    }

    /**
     * Method for submitting award sheet for approval
     * @param request
     * @param response
     * @return object of ModelAndView
     * @throws Exception
     */
    public ModelAndView withdrawRequest(HttpServletRequest request,
        HttpServletResponse response) throws Exception {
        AwardSheetInfoGetter inputObj = new AwardSheetInfoGetter();

        inputObj.setProgramCourseKey(request.getParameter("programCourseKey"));
        inputObj.setEntityId(request.getParameter("entityId"));
        inputObj.setCourseCode(request.getParameter("courseCode"));
        inputObj.setStartDate(request.getParameter("startDate"));
        inputObj.setEndDate(request.getParameter("endDate"));
        inputObj.setEmployeeCode(request.getParameter("employeeCode"));
        inputObj.setStatus(request.getParameter("status"));
        inputObj.setUniversityId(request.getParameter("entityId").substring(0, 4));

        String result = null;

        try {
            result = awardSheetDao.withdrawRequest(inputObj);
        } catch (MyException e) {
            System.out.println("in controller exception" + e.getMessage());

            return new ModelAndView("RegistrationForm/RegisterStudent",
                "result", e.getMessage());
        }

        return new ModelAndView("RegistrationForm/RegisterStudent", "result",
            result);
    }

    /**
     * Method for approving award blank sheet
     * @param request
     * @param response
     * @return object of ModelAndView
     * @throws Exception
     */
    public ModelAndView approveRequest(HttpServletRequest request,
        HttpServletResponse response) throws Exception {
        AwardSheetInfoGetter inputObj = new AwardSheetInfoGetter();

        inputObj.setProgramCourseKey(request.getParameter("programCourseKey"));
        inputObj.setEntityId(request.getParameter("entity"));
        inputObj.setCourseCode(request.getParameter("courseCode"));
        inputObj.setStartDate(request.getParameter("startDate"));
        inputObj.setEndDate(request.getParameter("endDate"));
        inputObj.setEmployeeCode(request.getParameter("employeeCode"));
        inputObj.setStatus(request.getParameter("status"));
        inputObj.setPreviousStatus(request.getParameter("previousStatus"));
        inputObj.setDisplayType(request.getParameter("displayType"));
        
        System.out.println("approveRequest:  "+inputObj.getProgramCourseKey());
        System.out.println("approveRequest:  "+inputObj.getEntityId());
        System.out.println("approveRequest:  "+inputObj.getCourseCode());
        System.out.println("approveRequest:  "+inputObj.getStartDate());
        System.out.println("approveRequest:  "+inputObj.getEndDate());
        System.out.println("approveRequest:  "+inputObj.getEmployeeCode());
        System.out.println("approveRequest:  "+inputObj.getStatus());
        System.out.println("approveRequest:  "+inputObj.getPreviousStatus());
        System.out.println("approveRequest:  "+inputObj.getDisplayType());

        String result = null;

        try {
            result = awardSheetDao.approveRequest(inputObj);
            System.out.println("value of result: "+ result);
        } catch (MyException e) {
        	System.out.println("I am inside catch: "+ e.getMessage());
            return new ModelAndView("RegistrationForm/RegisterStudent","result", e.getMessage());
        }
        return new ModelAndView("RegistrationForm/RegisterStudent", "result", result);
    }

    /**
     * Method for approving award blank sheet
     * @param request
     * @param response
     * @return object of ModelAndView
     * @throws Exception
     */
    public ModelAndView rejectRequest(HttpServletRequest request,
        HttpServletResponse response) throws Exception {
        AwardSheetInfoGetter inputObj = new AwardSheetInfoGetter();

        inputObj.setProgramCourseKey(request.getParameter("programCourseKey"));
        inputObj.setEntityId(request.getParameter("entity"));
        inputObj.setCourseCode(request.getParameter("courseCode"));
        inputObj.setStartDate(request.getParameter("startDate"));
        inputObj.setEndDate(request.getParameter("endDate"));
        inputObj.setEmployeeCode(request.getParameter("employeeCode"));
        inputObj.setStatus(request.getParameter("status"));
        inputObj.setDisplayType(request.getParameter("displayType")); // added by ankit

        String result = null;

        try {
            result = awardSheetDao.rejectRequest(inputObj);
        } catch (MyException e) {
            return new ModelAndView("RegistrationForm/RegisterStudent",
                "result", e.getMessage());
        }

        return new ModelAndView("RegistrationForm/RegisterStudent", "result",
            result);
    }
}
